#include "Group.h"
#include "../core/Impp.h"

namespace Impp {

Group::Group(db_id id, wxString screenName, const int & flags) :
	id(id),screenname(screenName),flags(flags)
{
}

Group::~Group()
{
}

db_id Group::getId(){
	return id;
}

wxString Group::getName(){
	return screenname;
}

void Group::addBuddy(Buddy* buddy){
	buddy->setGroup(this);
	for (size_t i = 0; i < buddies.size(); ++i) {
		if(buddies[i]==buddy){
			return;
		}
	}
	buddies.push_back(buddy);
}

void Group::removeBuddy(Buddy* buddy){
	std::vector<Buddy*>::iterator i(buddies.begin()); 
	for (; i != buddies.end(); ++i) { 
	      if(*i==buddy){
	    	  buddies.erase(i);
	    	  return;
	      }
	}
}

size_t Group::getSize(){
	return buddies.size();
}

void Group::setFlag(int flag){
	flags |= flag;
	Impp::exemplar()->getDatabase()->updateGroupFlags(this,flags);
}
void Group::clearFlag(int flag){
	flags &= ~flag;
	Impp::exemplar()->getDatabase()->updateGroupFlags(this,flags);
}

bool Group::hasFlag(int flag){
	return flags & flag;
}

size_t Group::getOnlineSize(){
	size_t count = 0;
	std::vector<Buddy*>::iterator iter(buddies.begin());
	for(; iter != buddies.end(); iter++){
		if((*iter)->getStatus() != OFFLINE && (*iter)->getStatus() != UNKNOWN){
			count++;
		}
	}
	return count;
}

}
